﻿namespace instamuz.DataAccess.Tests.QueriesTests
{
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading;
    using Domain.Entities;
    using IndyCode.Infrastructure.Domain;
    using Moq;
    using Xunit;
    using instamuz.Application.Criterias.Post;
    using instamuz.DataAccess.Queries.PostQueries;

    public class FindFeedForAccountQueryTest
    {
        [Fact]
        public void Test1()
        {
            var linqProvider = new Mock<ILinqProvider>();

            var account1 = new Account("email1", "login1") {Id = 1};
            var account2 = new Account("email2", "login2") {Id = 2};
            var account3 = new Account("email3", "login3") {Id = 3};

            account1.Post(new Post("desc1") {Id = 1});
            Thread.Sleep(50);
            account1.Post(new Post("desc2") {Id = 2});
            Thread.Sleep(50);
            account2.Post(new Post("desc3") {Id = 3});
            Thread.Sleep(50);
            account2.Post(new Post("desc4") {Id = 4});

            account3.Follow(account1);
            account3.Follow(account2);

            var postList = new List<Post>();
            postList.AddRange(account1.Posts);
            postList.AddRange(account2.Posts);

            linqProvider
                .Setup(x => x.Query<Post>())
                .Returns(postList.AsQueryable());

            var criteria = new FindFeedForAccountCriteria(account3);
            var result = new FindFeedForAccountQuery(linqProvider.Object).Ask(criteria);

            Assert.NotNull(result);

            var resultList = result.ToList();

            Assert.Equal(4, resultList.Count);
            Assert.Equal("desc4", resultList[0].Description);
            Assert.Equal("desc3", resultList[1].Description);
            Assert.Equal("desc2", resultList[2].Description);
            Assert.Equal("desc1", resultList[3].Description);
        }
    }
}